#include "Display.h"

Display::Display():

    _display(NULL),
    _event_queue(NULL),
    _timer(NULL),
    _font18(NULL),
    _FPS(40)

{
    //allegro init
    if(!al_init())
    {
        al_show_native_message_box(_display, NULL, NULL,
            "failed to initialize allegro!", NULL, NULL);
    }

    //display create
    _display = al_create_display(displayWidth,displayHeight);
    if(!_display)
    {
        al_show_native_message_box(_display, NULL, NULL,
            "failed to initialize display!", NULL, NULL);
    }

    //addons
    if(!al_init_primitives_addon())
    {
        al_show_native_message_box(_display, NULL, NULL,
            "failed to addon primitives!", NULL, NULL);
    }
    al_install_keyboard();
   	al_init_font_addon();
	al_init_ttf_addon();

   	//create event queue
    _event_queue = al_create_event_queue();
    if(!_event_queue)
    {
        al_show_native_message_box(_display, NULL, NULL,
            "failed to create even que!", NULL, NULL);
    }

    //create timer
    _timer = al_create_timer(1.0 / _FPS);
    if(!_timer)
    {
        al_show_native_message_box(_display, NULL, NULL,
            "failed to create timer!", NULL, NULL);
    }

    al_register_event_source(_event_queue, al_get_display_event_source(_display));
    al_register_event_source(_event_queue, al_get_timer_event_source(_timer));
    al_register_event_source(_event_queue, al_get_keyboard_event_source());

    load_font18();
    load_font36();
    load_font144();
}

void Display::start_timer()
{
    al_start_timer(_timer);
}

ALLEGRO_DISPLAY* Display::get_display()
{
    return _display;
}


ALLEGRO_EVENT_QUEUE* Display::get_event_queue()
{
    return _event_queue;
}

void Display::load_font18()
{
    _font18 = al_load_ttf_font("arial.ttf", 18, 0);
}

void Display::load_font36()
{
    _font36 = al_load_ttf_font("arial.ttf", 36, 0);
}

void Display::load_font144()
{
    _font144 = al_load_ttf_font("arial.ttf", 144, 0);
}

ALLEGRO_FONT* Display::get_font18()
{
    return _font18;
}

ALLEGRO_FONT* Display::get_font36()
{
    return _font36;
}

ALLEGRO_FONT* Display::get_font144()
{
    return _font144;
}

void Display::update()
{
    al_flip_display();
    al_clear_to_color(al_map_rgb(0,0,0));
}

Display::~Display()
{
    al_destroy_font(_font18);
    al_destroy_font(_font36);
    al_destroy_font(_font144);

    al_destroy_timer(_timer);
    al_destroy_event_queue(_event_queue);
    al_destroy_display(_display);
}

float Display::get_current_time()
{
    return al_current_time();
}
